﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Text.Editor;
using System.Windows.Media;
using System.Windows;
using System.Windows.Controls;

namespace Umc.Core.Tools.VSGesture.Shell
{
    public class GestureWpfWindow : MouseProcessorBase, IDisposable
    {
        private VSGestureAnalyzer analyzer = new VSGestureAnalyzer();
        private bool isMouseDown;
        private double preX, preY;

        IWpfTextView view;
        IAdornmentLayer layer;
        Image image;

		static bool isMouseDownIsCreatedHookObject = false;

        public GestureWpfWindow(IWpfTextView view)
        {
            this.view = view;

            Hook.HookManager.MouseDown += new System.Windows.Forms.MouseEventHandler(HookManager_MouseDown);
        }

        void HookManager_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
        }

        void HookManager_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            //if (isMouseDown == true)
            //{
            //    //NativeMethods.InvalidateRect(this.handler, IntPtr.Zero, true);
            //    //Application.DoEvents();


            //    ////e.Cancel = analyzer.Count > 10;

            //    //if (ee.Handled)
            //    //{
            //    //}
            //    //else
            //    //{
            //    //    //ee.Handled = false;
            //    //    //base.WndProc(ref preMsg);
            //    //}

            //    var ee = e as Hook.MouseEventExtArgs;
            //    ee.Handled = analyzer.Count > 10;

            //    //NativeMethods.mouse_event(0x206, 0, 0, 0, 0);

            //    analyzer.Analyzer();

            //    //NativeMethods.mouse_event(0x204, 0, 0, 0, 0);
            //    //NativeMethods.mouse_event(0x205, 0, 0, 0, 0);

            //}

            //Hook.HookManager.MouseUp -= new System.Windows.Forms.MouseEventHandler(HookManager_MouseUp);
            //IsCreatedHookObject = false;

            //isMouseDown = false;

            //layer = view.GetAdornmentLayer("VSGestureWindow");
            //layer.RemoveAllAdornments();

			Umc.Core.Tools.VSGesture.Hook.HookManager.MouseUp -= new System.Windows.Forms.MouseEventHandler(this.HookManager_MouseUp);
			isMouseDownIsCreatedHookObject = false;
			if (this.isMouseDown)
			{
				Umc.Core.Tools.VSGesture.Hook.MouseEventExtArgs args = e as Umc.Core.Tools.VSGesture.Hook.MouseEventExtArgs;
				if (args == null)
				{
					return;
				}
				args.Handled = this.analyzer.Count > 10;
				this.analyzer.Analyzer();
			}
			this.isMouseDown = false;
			this.layer = this.view.GetAdornmentLayer("VSGestureWindow");
			this.layer.RemoveAllAdornments();

        }

        public override void PreprocessGiveFeedback(GiveFeedbackEventArgs e)
        {
            base.PreprocessGiveFeedback(e);

            
        }

        //void VisualElement_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        public override void PostprocessMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
			try
			{
				if (Umc.Core.Tools.VSGesture.Services.VSGestureService.Current.VSGestureInfo.UserSettings.EnableVSGesture && (e.RightButton == System.Windows.Input.MouseButtonState.Pressed))
				{
					this.isMouseDown = true;
					Point position = e.GetPosition(this.view.VisualElement);
					this.preX = position.X + this.view.ViewportLeft;
					this.preY = position.Y + this.view.ViewportTop;
					e.Handled = true;
					if (!isMouseDownIsCreatedHookObject)
					{
						Umc.Core.Tools.VSGesture.Hook.HookManager.MouseUp += new System.Windows.Forms.MouseEventHandler(this.HookManager_MouseUp);
						isMouseDownIsCreatedHookObject = true;
					}
				}
			}
			catch (Exception exception)
			{
				MessageBox.Show(exception.Message);
			}

        }

        //void VisualElement_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        public override void PostprocessMouseMove(System.Windows.Input.MouseEventArgs e)
        {
			try
			{
				if (this.isMouseDown && (e.RightButton == System.Windows.Input.MouseButtonState.Pressed))
				{
					string lineColor = Umc.Core.Tools.VSGesture.Services.VSGestureService.Current.VSGestureInfo.UserSettings.LineColor;
					string lineThickness = Umc.Core.Tools.VSGesture.Services.VSGestureService.Current.VSGestureInfo.UserSettings.LineThickness;
					string[] strArray = lineColor.Split(new char[] { ',' });
					Point position = e.GetPosition(this.view.VisualElement);
					Brush brush = new SolidColorBrush(Color.FromRgb(byte.Parse(strArray[0]), byte.Parse(strArray[1]), byte.Parse(strArray[2])));
					this.analyzer.Add(new System.Windows.Input.StylusPoint(position.X, position.Y));
					this.layer = this.view.GetAdornmentLayer("VSGestureWindow");
					System.Windows.Shapes.Line adornment = new System.Windows.Shapes.Line
					{
						X1 = this.preX,
						Y1 = this.preY,
						X2 = position.X + this.view.ViewportLeft,
						Y2 = position.Y + this.view.ViewportTop,
						StrokeThickness = (float)((Umc.Core.Tools.VSGesture.Controls.LineThicknessStyle)Enum.Parse(typeof(Umc.Core.Tools.VSGesture.Controls.LineThicknessStyle), lineThickness)),
						Stroke = brush
					};
					this.layer.AddAdornment(AdornmentPositioningBehavior.ViewportRelative, null, null, adornment, null);
					this.preX = position.X + this.view.ViewportLeft;
					this.preY = position.Y + this.view.ViewportTop;
				}
			}
			catch (Exception exception)
			{
				MessageBox.Show(exception.Message);
			}
			base.PostprocessMouseMove(e);

        }



        public void Dispose()
        {
            analyzer = null;
            //pen = null;
        }
    }
}
